<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>msgrcv</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_007_817">&nbsp;</a>NAME</h4><blockquote>
msgrcv - message receive operation
</blockquote><h4><a name = "tag_000_007_818">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="sysmsg.h.html">sys/msg.h</a>&gt;

ssize_t msgrcv(int <i>msqid</i>, void *<i>msgp</i>, size_t <i>msgsz</i>, long int <i>msgtyp</i>,
    int <i>msgflg</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_007_819">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>msgrcv()</i>
function reads a message from the queue
associated with the message queue identifier specified by
<i>msqid</i>
and places it in the user-defined buffer pointed to by
<i>msgp</i>.
<p>
The argument
<i>msgp</i>
points to a user-defined buffer that must contain
first a field of type
<b>long int</b>
that will specify the type of the message,
and then a data portion that will hold the data bytes of the message.
The structure below is an example of what
this user-defined buffer might look like:
<pre>
<code>
struct mymsg {
    long int    mtype;     /* message type */
    char        mtext[1];  /* message text */
}
</code>
</pre>
<p>
The structure member
<b>mtype</b>
is the received message's type as specified by the sending process.
<p>
The structure member
<b>mtext</b>
is the text of the message.
<p>
The argument
<i>msgsz</i>
specifies the size in bytes of
<b>mtext</b>.
The received message is truncated to
<i>msgsz</i>
bytes if it is larger than
<i>msgsz</i>
and (<i>msgflg</i> &amp; MSG_NOERROR) is non-zero.
The truncated part of the message is lost
and no indication of the truncation is given to the calling process.
<p>
If the value of 
<i>msgsz</i>
is greater than {SSIZE_MAX}, the result is implementation-dependent.
<p>
The argument
<i>msgtyp</i>
specifies the type of message requested as follows:
<ul>
<p>
<li>
If
<i>msgtyp</i>
is 0, the first message on the queue is received.
<p>
<li>
If
<i>msgtyp</i>
is greater than 0, the first message of type
<i>msgtyp</i>
is received.
<p>
<li>
If
<i>msgtyp</i>
is less than 0, the first message of the lowest
type that is less than or equal to the absolute value of
<i>msgtyp</i>
is received.
<p>
</ul>
<p>
The argument
<i>msgflg</i>
specifies the action to be taken if a
message of the desired type is not on the queue.
These are as follows:
<ul>
<p>
<li>
If (<i>msgflg</i> &amp; IPC_NOWAIT)
is non-zero, the calling thread will return
immediately with a return value of -1 and
<i>errno</i>
set to [ENOMSG].
<br>
<p>
<li>
If (<i>msgflg</i> &amp; IPC_NOWAIT)
is 0, the calling thread will suspend execution
until one of the following occurs:
<ul>
<p>
<li>
A message of the desired type is placed on the queue.
<p>
<li>
The message queue identifier
<i>msqid</i>
is removed from the system;
when this occurs,
<i>errno</i>
is set equal to [EIDRM] and -1 is returned.
<p>
<li>
The calling thread receives a signal that is to be caught;
in this case a message is not received and
the calling thread resumes execution in the manner prescribed in
<i><a href="sigaction.html">sigaction()</a></i>.
<p>
</ul>
<p>
</ul>
<p>
Upon successful completion, the following actions are taken
with respect to the data structure associated with
<i>msqid</i>:
<ul>
<p>
<li>
<b>msg_qnum</b>
is decremented by 1.
<p>
<li>
<b>msg_lrpid</b>
is set equal to the process ID of the calling process.
<p>
<li>
<b>msg_rtime</b>
is set equal to the current time.
<p>
</ul>
</blockquote><h4><a name = "tag_000_007_820">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>msgrcv()</i>
returns a value equal to the number of bytes actually placed into the buffer
<i>mtext</i>.
Otherwise, no message will be received,
<i>msgrcv()</i>
will return (<b>ssize_t</b>)-1 and
<i>errno</i>
will be set to indicate the error.
</blockquote><h4><a name = "tag_000_007_821">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>msgrcv()</i>
function will fail if:
<dl compact>

<dt>[E2BIG]<dd>
The value of
<b>mtext</b>
is greater than
<i>msgsz</i>
and (<i>msgflg</i> &amp; MSG_NOERROR)
is 0.

<dt>[EACCES]<dd>
Operation permission is denied to the calling process.  See
<a href="ipc.html">IPC</a>.

<dt>[EIDRM]<dd>
The message queue identifier
<i>msqid</i>
is removed from the system.

<dt>[EINTR]<dd>
The
<i>msgrcv()</i>
function was interrupted by a signal.

<dt>[EINVAL]<dd>
<i>msqid</i>
is not a valid message queue identifier.

<dt>[ENOMSG]<dd>
The queue does not contain a message of the desired type and
(<i>msgflg</i> &amp; IPC_NOWAIT) is non-zero.

</dl>
</blockquote><h4><a name = "tag_000_007_822">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_007_823">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The POSIX Realtime Extension defines alternative interfaces for interprocess communication.
Application developers who need to use IPC should design their
applications so that modules using the IPC routines described in
<a href="ipc.html">IPC</a>
can be easily modified to use the alternative interfaces.
</blockquote><h4><a name = "tag_000_007_824">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_007_825">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="mq_close.html">mq_close()</a></i>,
<i><a href="mq_getattr.html">mq_getattr()</a></i>,
<i><a href="mq_notify.html">mq_notify()</a></i>,
<i><a href="mq_open.html">mq_open()</a></i>,
<i><a href="mq_receive.html">mq_receive()</a></i>,
<i><a href="mq_send.html">mq_send()</a></i>,
<i><a href="mq_setattr.html">mq_setattr()</a></i>,
<i><a href="mq_unlink.html">mq_unlink()</a></i>,
<i><a href="msgctl.html">msgctl()</a></i>,
<i><a href="msgget.html">msgget()</a></i>,
<i><a href="msgsnd.html">msgsnd()</a></i>,
<i><a href="sigaction.html">sigaction()</a></i>,
<i><a href="sysmsg.h.html">&lt;sys/msg.h&gt;</a></i>,
<a href="ipc.html">IPC</a>.
<br>
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 2 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

